package com.linkage.usercenter.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.linkage.common.constant.CommonSysConstant;
import com.linkage.common.exception.ServiceException;
import com.linkage.usercenter.dao.*;
import com.linkage.usercenter.pojo.entity.*;
import com.linkage.usercenter.pojo.vo.DptAuditVo;
import com.linkage.usercenter.pojo.vo.DptInfoVO;
import com.linkage.usercenter.pojo.vo.PersonAuditVo;
import com.linkage.usercenter.pojo.vo.UserVO;
import com.linkage.usercenter.service.IDepService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DepOperationServiceImpl implements IDepService {

    private static final Logger logger = LoggerFactory.getLogger(DepOperationServiceImpl.class);

    @Autowired
    private CoreUserMapper userDao;

    @Autowired
    private CorePersonInfoMapper personDao;


    @Autowired
    private CoreDptInfoMapper dptInfoDao;

    @Autowired
    private DptSubInfoMapper dptSubInfoMapper;

    @Resource
    private UserSubInfoMapper userSubInfoMapper;

    // //根据机构查询部门信息
    @Override
    public List<DptInfoVO> getDptList(String orgCode) {
        List<DptInfoVO> resultList = new ArrayList<DptInfoVO>();
        List<CoreDptInfo> dptlist = dptInfoDao.queryByOrgCode(orgCode);
        if(dptlist.isEmpty()){
            dptlist = dptInfoDao.queryByDptCode(orgCode);
            if(dptlist.isEmpty()){
                return resultList;
            }else{
                for (CoreDptInfo coreDptInfo : dptlist) {
                    this.addSubDptInfo(dptlist, coreDptInfo);
                }

            }
        }
        if(!dptlist.isEmpty()){
            dptlist.forEach(x ->{
                DptInfoVO dptInfoVO = new DptInfoVO();
                BeanUtil.copyProperties(x, dptInfoVO);
                resultList.add(dptInfoVO);
            });
        }
        return resultList;
    }

    //添加子机构
    private void addSubDptInfo(List<CoreDptInfo> dptlist, CoreDptInfo coreDptInfo) {
        if(coreDptInfo == null){
            return;
        }
        List<CoreDptInfo> subDptList = dptInfoDao.querySubRecordByDptCode(coreDptInfo.getDptCode());
        if(!subDptList.isEmpty()){
            for (CoreDptInfo dptInfo : subDptList) {
                dptlist.add(dptInfo);
                this.addSubDptInfo(dptlist, dptInfo);
            }
        }

    }


    @Override
    public List<DptInfoVO> getDptListByMobile(String userMobile) {
        String orgCode = personDao.getOrgCodeByMobile(userMobile);
        List<CoreDptInfo> dptlist = dptInfoDao.queryByOrgCode(orgCode);
        List<DptInfoVO> resultList = new ArrayList<DptInfoVO>();
        if(dptlist.isEmpty()){
            dptlist = dptInfoDao.queryByDptCode(orgCode);
            if(dptlist.isEmpty()){
                return resultList;
            }else{
                for (CoreDptInfo coreDptInfo : dptlist) {
                    this.addSubDptInfo(dptlist, coreDptInfo);
                }

            }
        }
        if(!dptlist.isEmpty()){
            dptlist.forEach(x ->{
                DptInfoVO dptInfoVO = new DptInfoVO();
                BeanUtil.copyProperties(x, dptInfoVO);
                resultList.add(dptInfoVO);
            });
        }
        return resultList;
    }

    ////根据机构查询当前机构人员信息
    @Override
    public List<UserVO> getUsreListByOrgCode(String orgCode) {
        List<UserVO> resultList = new ArrayList<UserVO>();
        List<CoreUser> userList = userDao.queryCoreUserByOrgCode(orgCode);
        if(userList.isEmpty()){
            userList = userDao.queryCoreUserByDptCode(orgCode);
        }
        if(!userList.isEmpty()){
            userList.forEach( x->{
                UserVO userVO = new UserVO();
                BeanUtil.copyProperties(x, userVO);
                UserSubInfo userSubInfo = userSubInfoMapper.selectByPrimaryKey(userVO.getUserCode());
                if(userSubInfo != null){
                    userVO.setUserPhotoUrl(userSubInfo.getUserPhotoUrl());
                }
                resultList.add(userVO);
            });
        }
        return resultList;
    }

    @Override
    public List<UserVO> getUsreListByRoleCode(String roleCode) {
        List<UserVO> resultList = new ArrayList<UserVO>();
        List<CoreUser> userList = userDao.queryCoreUserByRoleCode(roleCode);
        if(!userList.isEmpty()){
            userList.forEach( x->{
                UserVO userVO = new UserVO();
                BeanUtil.copyProperties(x, userVO);
                resultList.add(userVO);
            });
        }
        return resultList;
    }

    @Override
    public List queryAuditList(String status){
        List list = dptSubInfoMapper.queryPersonAuditList(status);
        return list;
    }

    @Override
    public void audit(String status,String code,String expireTime){
        CoreUser user = userDao.getRecordByUserCode(code,null);
        String personCode = ObjectUtil.isEmpty(user)?null:user.getUserPersonCode();
        if(StrUtil.isEmpty(personCode)){
            throw new ServiceException(1,"未查询到用户信息");
        }
        personDao.updatePersonJobFlag(personCode,status);
        UserSubInfo userSubInfo = userSubInfoMapper.selectByPrimaryKey(personCode);
        Date expire = null;
        if(StrUtil.equals("0",status)){
            expire = DateUtil.parse(expireTime,"yyyy-MM-dd HH:mm:ss");
        }
        if(ObjectUtil.isNull(userSubInfo)){
            userSubInfo = new UserSubInfo();
            userSubInfo.setUserCode(personCode);
            userSubInfo.setUserExpireTime(expire);
            userSubInfo.setUserSubFlag("1");
            userSubInfoMapper.insertSelective(userSubInfo);
        }else{
            userSubInfo.setUserExpireTime(expire);
            userSubInfoMapper.updateByPrimaryKeySelective(userSubInfo);
        }
        logger.info("审核保安信息结束");
    }

    @Override
    public Object auditInfo(String code){
        return dptSubInfoMapper.queryPersonAuditInfo(code);
    }

    @Override
    public List<DptInfoVO> getDptListByName(String dptName){
        return dptInfoDao.queryDptListByName(CommonSysConstant.ORG_CODE_TYPE_5,dptName);
    }
}
